﻿window.addEventListener('keydown', (e) => {
    e.cancelBubble = true
    if (e.key === 'F12') {
        showDevTools()
    }
    else if (e.key === 'F5') {
        location.reload(e.ctrlKey)
    }
    else if (e.key === 'w' && e.ctrlKey) {
        parent.tab.close();
    }
})

function resize() {
    const height = document.body.scrollHeight;
    parent.document.querySelectorAll('iframe').forEach((it) => {
        if (it.contentWindow === window) {
            it.setAttribute('style', 'height:' + height + 'px');
        }
    });
}
function getFrame() {
    parent.document.querySelectorAll('iframe').forEach((it) => {
        if (it.contentWindow === window) {
            return it;
        }
    })
}
window.addEventListener('load', (e) => {
    if (location.href.match('[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}/\\d+.html')) {
        access(location.href);
    }
    window.focus();
    parent.document.querySelector('.tab-content').scrollTo(0, 0);
    resize()
    if (document.querySelector('#result')) {
        document.querySelector('#result').addEventListener('change', resize)
    }
})
window.addEventListener('resize', resize)
async function getFetch() {
    let url, data, errorCallback
    for (let i = 0; i < arguments.length; i++) {
        if (arguments.length <= 0) {
            console.error('Get function arguments errors')
            return
        }
        switch (typeof arguments[i]) {
            case 'string':
                url = arguments[i]
                break
            case 'object':
                data = arguments[i]
                break
            case 'function':
                errorCallback = arguments[i]
                break
        }
    }
    let headers = new Headers()
    if (!data) {
        headers.append('Content-Type', 'text/html;charset=utf-8')
    }
    else {
        headers.append('Content-Type', 'application/json')
    }
    let options = {
        method: 'GET',
        credentials: 'include',
        headers: headers
    }
    if (data) {
        options.body = JSON.stringify(data)
        options.method = 'POST'
    }
    try {
        const response = await fetch(url, options)
        let text
        const contentType = response.headers.get('Content-Type')
        if (contentType.match(/charset=gbk/i)) {
            text = await response.blob().then(blob => {
                return new Promise((resolve, reject) => {
                    var reader = new FileReader()
                    reader.onload = function (e) {
                        data ? resolve(JSON.parse(reader.result)) : resolve(reader.result)
                    }
                    reader.onerror = function (err) {
                        console.error(err)
                        if (errorCallback) {
                            errorCallback(err)
                        }
                    }
                    reader.readAsText(blob, 'GBK')
                })
            })
        }
        else {
            text = await response.text()
        }
        return data ? JSON.parse(text) : text
    }
    catch (err) {
        console.error(err)
        if (errorCallback) {
            errorCallback(err)
        }
    }
}
HTMLAnchorElement.prototype.toUrl = function (baseUrl) {
    return this.href.toUrl(baseUrl)
}
String.prototype.toUrl = function (baseUrl) {
    let startUrl = this.match('[^:]*://[^/]*/')
    let url = this.replace(/\\/g, '/')
    if (url.indexOf(startUrl) !== -1) {
        return baseUrl + url.replace(startUrl, '')
    }
    else if (url.substring(0, 2) === '//') {
        return url
    }
    else if (url.indexOf('//') !== -1) {
        return url
    }
    else {
        return baseUrl + url
    }
}
String.prototype.getInfoUrl = function (scheme, host) {
    const m = this.match('://([^/]*)/(.*)')
    if (m[1] && m[2]) {
        return scheme + '://' + m[1] + '/' + m[2]
    }
    let url = this
    if (url.indexOf('//') !== -1) {
        url = url.substring(url.indexOf('//') + 2)
    }
    if (url.indexOf('/') !== -1) {
        url = url.substring(url.indexOf('/'))
    }
    return scheme + '://' + host + url
}
Element.prototype.formatToString = function () {
    let context = this.outerHTML.replace(/<br[^>]*>/g, '\n')
    let element = document.createElement('div')
    element.innerHTML = context
    const lines = element.innerText.split('\n')
    context = ''
    for (let i = 0; i < lines.length; i++) {
        if (lines[i].trim() !== '') {
            context += lines[i].trim().replace(/&nbsp;/ig, '') + '\n'
        }
    }
    return context
}